package order

import (
	"com.cyt.yunpay/app/model"
	"com.cyt.yunpay/app/service/pay"
	"com.cyt.yunpay/library/response"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
	"github.com/gogf/gf/os/gtime"
	"time"
)

// 订单API管理对象
var Order = new(orderApi)

type orderApi struct{}

// 充值
func (a *orderApi) Recharge(r *ghttp.Request) {
	var (
		apiReq *model.RechargeReq
	)

	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	if apiReq.OrderExpireDatetime == nil {
		apiReq.OrderExpireDatetime = gtime.Now().Add(86400 * time.Second)
	}
	params := g.Map{
		"BizOrderNo":          apiReq.OrderSn,
		"BizUserId":           apiReq.Username,
		"AccountSetNo":        apiReq.AccountSetNo,
		"Amount":              apiReq.Amount,
		"Fee":                 apiReq.Fee,
		"ValidateType":        apiReq.ValidateType,
		"BackUrl":             apiReq.BackUrl,
		"OrderExpireDatetime": apiReq.OrderExpireDatetime,
		"PayMethod":           apiReq.PayMethod,
		"GoodsName":           apiReq.GoodsName,
		"IndustryCode":        apiReq.IndustryCode,
		"IndustryName":        apiReq.IndustryName,
		"Source":              apiReq.Source,
		"Summary":             apiReq.Summary,
		"ExtendInfo":          apiReq.ExtendInfo,
	}

	data, err := pay.CommonPay("allinpay", "OrderDepositApply", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 提现
func (a *orderApi) Withdraw(r *ghttp.Request) {
	var (
		apiReq *model.WithdrawReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizOrderNo":          apiReq.BizOrderNo,
		"BizUserId":           apiReq.BizUserId,
		"AccountSetNo":        apiReq.AccountSetNo,
		"Amount":              apiReq.Amount,
		"Fee":                 apiReq.Fee,
		"ValidateType":        apiReq.ValidateType,
		"BackUrl":             apiReq.BackUrl,
		"OrderExpireDatetime": apiReq.OrderExpireDatetime,
		"PayMethod":           apiReq.PayMethod,
		"BankCardNo":          apiReq.BankCardNo,
		"BankCardPro":         apiReq.BankCardPro,
		"WithdrawType":        apiReq.WithdrawType,
		"IndustryCode":        apiReq.IndustryCode,
		"IndustryName":        apiReq.IndustryName,
		"Source":              apiReq.Source,
		"Summary":             apiReq.Summary,
		"ExtendInfo":          apiReq.ExtendInfo,
	}
	data, err := pay.CommonPay("allinpay", "OrderWithdrawApply", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 创建订单
func (a *orderApi) CreateOrder(r *ghttp.Request) {
	var (
		apiReq *model.CreateOrderReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"PayerId":             apiReq.PayerId,
		"RecieverId":          apiReq.RecieverId,
		"BizOrderNo":          apiReq.BizOrderNo,
		"Amount":              apiReq.Amount,
		"Fee":                 apiReq.Fee,
		"ValidateType":        apiReq.ValidateType,
		"SplitRule":           apiReq.SplitRule,
		"FrontUrl":            apiReq.FrontUrl,
		"BackUrl":             apiReq.BackUrl,
		"OrderExpireDatetime": apiReq.OrderExpireDatetime,
		"PayMethod":           apiReq.PayMethod,
		"GoodsType":           apiReq.GoodsType,
		"BizGoodsNo":          apiReq.BizGoodsNo,
		"GoodsName":           apiReq.GoodsName,
		"GoodsDesc":           apiReq.GoodsDesc,
		"IndustryCode":        apiReq.IndustryCode,
		"IndustryName":        apiReq.IndustryName,
		"Source":              apiReq.Source,
		"Summary":             apiReq.Summary,
		"ExtendInfo":          apiReq.ExtendInfo,
	}
	data, err := pay.CommonPay("allinpay", "OrderConsumeApply", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 托管代收申请(标准版)
func (a *orderApi) AgentCollectApply(r *ghttp.Request) {
	var (
		apiReq *model.AgentCollectApplyReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizOrderNo":          apiReq.BizOrderNo,
		"PayerId":             apiReq.PayerId,
		"RecieverList":        apiReq.RecieverList,
		"GoodsType":           apiReq.GoodsType,
		"BizGoodsNo":          apiReq.BizGoodsNo,
		"TradeCode":           apiReq.TradeCode,
		"Amount":              apiReq.Amount,
		"Fee":                 apiReq.Fee,
		"ValidateType":        apiReq.ValidateType,
		"FrontUrl":            apiReq.FrontUrl,
		"BackUrl":             apiReq.BackUrl,
		"OrderExpireDatetime": apiReq.OrderExpireDatetime,
		"PayMethod":           apiReq.PayMethod,
		"GoodsName":           apiReq.GoodsName,
		"GoodsDesc":           apiReq.GoodsDesc,
		"IndustryCode":        apiReq.IndustryCode,
		"IndustryName":        apiReq.IndustryName,
		"Source":              apiReq.Source,
		"Summary":             apiReq.Summary,
		"ExtendInfo":          apiReq.ExtendInfo,
	}
	data, err := pay.CommonPay("allinpay", "OrderAgentCollectApply", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 单笔托管代付(标准版)
func (a *orderApi) SignalAgentPay(r *ghttp.Request) {
	var (
		apiReq *model.SignalAgentPayReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizOrderNo":     apiReq.BizOrderNo,
		"CollectPayList": apiReq.CollectPayList,
		"BizUserId":      apiReq.BizUserId,
		"AccountSetNo":   apiReq.AccountSetNo,
		"BackUrl":        apiReq.BackUrl,
		"Amount":         apiReq.Amount,
		"Fee":            apiReq.Fee,
		"SplitRuleList":  apiReq.SplitRuleList,
		"GoodsType":      apiReq.GoodsType,
		"BizGoodsNo":     apiReq.BizGoodsNo,
		"TradeCode":      apiReq.TradeCode,
		"Summary":        apiReq.Summary,
		"ExtendInfo":     apiReq.ExtendInfo,
	}
	data, err := pay.CommonPay("allinpay", "OrderSignalAgentPay", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 批量托管代付(标准版)
func (a *orderApi) BatchAgentPay(r *ghttp.Request) {
	var (
		apiReq *model.BatchAgentPayReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizBatchNo":   apiReq.BizBatchNo,
		"BatchPayList": apiReq.BatchPayList,
		"GoodsType":    apiReq.GoodsType,
		"BizGoodsNo":   apiReq.BizGoodsNo,
		"TradeCode":    apiReq.TradeCode,
	}
	data, err := pay.CommonPay("allinpay", "OrderBatchAgentPay", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 确认支付 (分前台和后台)
func (a *orderApi) ConfirmPay(r *ghttp.Request) {
	var (
		apiReq *model.PayReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizUserId":        apiReq.BizUserId,
		"BizOrderNo":       apiReq.BizOrderNo,
		"TradeNo":          apiReq.TradeNo,
		"JumpUrl":          apiReq.JumpUrl,
		"VerificationCode": apiReq.VerificationCode,
		"ConsumerIp":       apiReq.ConsumerIp,
	}
	data, err := pay.CommonPay("allinpay", "OrderPay", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 支付码页
func (a *orderApi) GetPayCodePage(r *ghttp.Request) {
	var (
		apiReq *model.PayReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizUserId":        apiReq.BizUserId,
		"BizOrderNo":       apiReq.BizOrderNo,
		"TradeNo":          apiReq.TradeNo,
		"JumpUrl":          apiReq.JumpUrl,
		"VerificationCode": apiReq.VerificationCode,
		"ConsumerIp":       apiReq.ConsumerIp,
	}
	data, err := pay.CommonPay("allinpay", "OrderGetPayCodePage", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 支付密码页
func (a *orderApi) GetPayPasswordPage(r *ghttp.Request) {
	var (
		apiReq *model.PayReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizUserId":        apiReq.BizUserId,
		"BizOrderNo":       apiReq.BizOrderNo,
		"TradeNo":          apiReq.TradeNo,
		"JumpUrl":          apiReq.JumpUrl,
		"VerificationCode": apiReq.VerificationCode,
		"ConsumerIp":       apiReq.ConsumerIp,
	}
	data, err := pay.CommonPay("allinpay", "OrderGetPayPasswordPage", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 商品录入
func (a *orderApi) EntryGoods(r *ghttp.Request) {
	var (
		apiReq *model.EntryGoodsReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizUserId":   apiReq.BizUserId,
		"GoodsType":   apiReq.GoodsType,
		"BizGoodsNo":  apiReq.BizGoodsNo,
		"GoodsName":   apiReq.GoodsName,
		"GoodsDetail": apiReq.GoodsDetail,
		"GoodsParams": apiReq.GoodsParams,
		"ShowUrl":     apiReq.ShowUrl,
	}
	data, err := pay.CommonPay("allinpay", "OrderEntryGoods", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 商品查询
func (a *orderApi) QueryGoods(r *ghttp.Request) {
	var (
		apiReq *model.QueryGoodsReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizUserId":  apiReq.BizUserId,
		"BizGoodsNo": apiReq.BizGoodsNo,
	}
	data, err := pay.CommonPay("allinpay", "OrderQueryGoods", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 冻结金额
func (a *orderApi) FreezeMoney(r *ghttp.Request) {
	var (
		apiReq *model.FreezeMoneyReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizUserId":    apiReq.BizUserId,
		"BizFreezenNo": apiReq.BizFreezenNo,
		"AccountSetNo": apiReq.AccountSetNo,
		"Amount":       apiReq.Amount,
	}
	data, err := pay.CommonPay("allinpay", "OrderFreezeMoney", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 解冻金额
func (a *orderApi) UnfreezeMoney(r *ghttp.Request) {
	var (
		apiReq *model.FreezeMoneyReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizUserId":    apiReq.BizUserId,
		"BizFreezenNo": apiReq.BizFreezenNo,
		"AccountSetNo": apiReq.AccountSetNo,
		"Amount":       apiReq.Amount,
	}
	data, err := pay.CommonPay("allinpay", "OrderUnfreezeMoney", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 退款申请
func (a *orderApi) Refund(r *ghttp.Request) {
	var (
		apiReq *model.RefundReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizOrderNo":    apiReq.BizOrderNo,
		"OriBizOrderNo": apiReq.OriBizOrderNo,
		"BizUserId":     apiReq.BizUserId,
		"RefundType":    apiReq.RefundType,
		"RefundList":    apiReq.RefundList,
		"BackUrl":       apiReq.BackUrl,
		"Amount":        apiReq.Amount,
		"CouponAmount":  apiReq.CouponAmount,
		"FeeAmount":     apiReq.FeeAmount,
		"ExtendInfo":    apiReq.ExtendInfo,
	}
	data, err := pay.CommonPay("allinpay", "OrderRefund", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 平台转账
func (a *orderApi) ApplicationTransfer(r *ghttp.Request) {
	var (
		apiReq *model.ApplicationTransferReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizTransferNo":      apiReq.BizTransferNo,
		"SourceAccountSetNo": apiReq.SourceAccountSetNo,
		"TargetBizUserId":    apiReq.TargetBizUserId,
		"TargetAccountSetNo": apiReq.TargetAccountSetNo,
		"Amount":             apiReq.Amount,
		"ExtendInfo":         apiReq.ExtendInfo,
	}
	data, err := pay.CommonPay("allinpay", "OrderApplicationTransfer", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 查询余额
func (a *orderApi) QueryBalance(r *ghttp.Request) {
	var (
		apiReq *model.QueryBalanceReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizUserId":    apiReq.BizUserId,
		"AccountSetNo": apiReq.AccountSetNo,
	}
	data, err := pay.CommonPay("allinpay", "OrderQueryBalance", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 查询账户收支明细
func (a *orderApi) QueryInExpDetail(r *ghttp.Request) {
	var (
		apiReq *model.QueryInExpDetailReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizUserId":     apiReq.BizUserId,
		"AccountSetNo":  apiReq.AccountSetNo,
		"DateStart":     apiReq.DateStart,
		"DateEnd":       apiReq.DateEnd,
		"StartPosition": apiReq.StartPosition,
		"QueryNum":      apiReq.QueryNum,
	}
	data, err := pay.CommonPay("allinpay", "OrderQueryInExpDetail", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 付款方资金代付明细查询
func (a *orderApi) GetPaymentInformationDetail(r *ghttp.Request) {
	var (
		apiReq *model.PaymentInformationReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizUserId":    apiReq.BizUserId,
		"BizOrderNo":   apiReq.BizOrderNo,
		"AccountSetNo": apiReq.AccountSetNo,
		"DateStart":    apiReq.DateStart,
		"DateEnd":      apiReq.DateEnd,
	}
	data, err := pay.CommonPay("allinpay", "OrderGetPaymentInformationDetail", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 收款方在途资金明细查询
func (a *orderApi) GetPayeeFundsInTransitDetail(r *ghttp.Request) {
	var (
		apiReq *model.PaymentInformationReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizUserId":    apiReq.BizUserId,
		"BizOrderNo":   apiReq.BizOrderNo,
		"AccountSetNo": apiReq.AccountSetNo,
		"DateStart":    apiReq.DateStart,
		"DateEnd":      apiReq.DateEnd,
	}
	data, err := pay.CommonPay("allinpay", "OrderGetPayeeFundsInTransitDetail", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 订单分账明细查询
func (a *orderApi) GetOrderSplitRuleListDetail(r *ghttp.Request) {
	var (
		apiReq *model.OrderNoReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizOrderNo": apiReq.BizOrderNo,
	}
	data, err := pay.CommonPay("allinpay", "OrderGetOrderSplitRuleListDetail", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 重发支付短信验证码
func (a *orderApi) ResendPaySMS(r *ghttp.Request) {
	var (
		apiReq *model.OrderNoReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizOrderNo": apiReq.BizOrderNo,
	}
	data, err := pay.CommonPay("allinpay", "OrderResendPaySMS", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// 订单关闭
func (a *orderApi) CloseOrder(r *ghttp.Request) {
	var (
		apiReq *model.OrderNoReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := g.Map{
		"BizOrderNo": apiReq.BizOrderNo,
	}
	data, err := pay.CommonPay("allinpay", "OrderCloseOrder", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}
